package be.xios.projectquiz.jury;

import java.io.IOException;
import javax.swing.JFrame;
import be.xios.projectquiz.game.Blokken;
import be.xios.projectquiz.game.BlokkenJuryGUI;
import be.xios.projectquiz.game.Game;
import be.xios.projectquiz.global.SharedItems;
import be.xios.projectquiz.global.StreamMethods;

/// <summary>
/// Deze klasse verzorgt het spelverloop van blokken tussen de jury en server
/// </summary>
/// <author>Sebastian Kubski</author>
public class JuryClientBlokken implements Runnable {

	private BlokkenJuryGUI gameGUI;
	private StreamMethods streamMethods;
	private Game currentGame;
	private SharedItems shared;
	
	public JuryClientBlokken(Game currentGame, StreamMethods streamMethods, SharedItems shared) {
		this.currentGame=currentGame;
		this.streamMethods=streamMethods;
		this.shared=shared;
	}
	
	@Override
	public void run() {
		/*GlobalMethods gm = new GlobalMethods();
		int []values = new int[2];
		values=gm.findValuesFullScreen(); */
		gameGUI = new BlokkenJuryGUI(currentGame);
		gameGUI.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		gameGUI.setSize(400,400);
		gameGUI.setLocation(0,0);
		//gameGUI.setSize(values[0], values[1]);
		gameGUI.setVisible(true);
		
		try {
			//Stap 5.1
			streamMethods.openObjectInputStream();
			gameGUI.setUpdatedGame((Blokken) streamMethods.getObjectInput().readObject());
			
			System.out.println("CLIENTSIDE: Jury received game with answers");		
		}
		catch (ClassNotFoundException e) {
			e.printStackTrace();
		} 
		catch (IOException e) {
			e.printStackTrace();
		}
		
		while( gameGUI.getInitialGame() == null ) {
			
			try {
				Thread.sleep(1000);
			} 
			catch (InterruptedException e1) {
				e1.printStackTrace();
			}
			
			if ( gameGUI.getInitialGame() != null) {
				try {
					streamMethods.openObjectOutputStream();
					streamMethods.getObjectOutput().writeObject((Blokken)gameGUI.getInitialGame());
					System.out.println( "JURY has sent a game with updated playerList" + gameGUI.getInitialGame());
				}
				catch (IOException e) {
					e.printStackTrace();
				}
			}
		}		
		gameGUI.setInitialGame(null);

	}//Einde run
	
	

}
